﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using Domain.DAL;
using Resources;

namespace WebApp
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUsername.Focus();
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckNewPassword())
                {
                    Utility.ShowMessageBox(this.Page, Strings.MissMatchPassword);
                    return;
                }

                string username = txtUsername.Text;
                string oldPassword = Utility.HashMD5(txtOldPassword.Text);
                string newPassword = Utility.HashMD5(txtNewPassword.Text);

                UserDAO uDAO = UserDAO.GetInstance();
                if (uDAO != null)
                {
                    if (uDAO.ChangePassword(username, oldPassword, newPassword))
                    {
                        Utility.ShowMessageBox(this.Page, Strings.ChangedPassword);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Strings.InvalidUsernameOrPassword);
                    }
                }

            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/Default.aspx"));
        }

        private bool CheckNewPassword()
        {
            return txtNewPassword.Text.Equals(txtConfirmPassword.Text);
        }
    }
}